package com.changgou.util;

import com.changgou.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.net.InetAddress;

/**
 * 封装FastDFS工具类
 */
public class FastDFSClient {

    /**
     *   初始化FastDFS配置
     */
    static {
        String path = "fdfs_client.conf";
        String conf_filename = new ClassPathResource(path).getPath();
        try {
            ClientGlobal.init(conf_filename);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     *
     * @return
     */
    public static String[] uploadFile(FastDFSFile fastDFSFile) {
        try {
            //上传文件相关属性
            byte[] file_buff = fastDFSFile.getContent();
            String file_ext = fastDFSFile.getExt();
            NameValuePair[] meta_list = new NameValuePair[1];
            meta_list[0] =new NameValuePair(fastDFSFile.getAuthor());

            //创建文件跟踪对象客户端
            TrackerClient trackerClient = new TrackerClient();
            //获取文件跟踪对象
            TrackerServer trackerServer = trackerClient.getConnection();
            //创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            //文件上传
            String[] upload_file = storageClient.upload_file(file_buff, file_ext, meta_list);
            return upload_file;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取跟踪服务器地址
    public static String getUrl(){
        try {
            //创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();

            int port = ClientGlobal.getG_tracker_http_port();
            InetAddress ip = trackerServer.getInetSocketAddress().getAddress();

            String url = "http://"+ ip +":" + port ;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文件下载
     * @param group_name   组名
     * @param remote_filename 文件名
     * @return
     */
    public static byte[] downloadFile(String group_name,String remote_filename){
        try {
            //创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            //获取跟踪服务器客户端
            TrackerServer trackerServer = trackerClient.getConnection();

            //创建存储服务客户端
            StorageClient storageClient = new StorageClient(trackerServer,null);
            byte[] bytes = storageClient.download_file(group_name, remote_filename);
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文件删除
     * @param group_name 组名
     * @param remote_filename 文件名
     */
    public static void deleteFile(String group_name,String remote_filename){
        try {
            //创建跟踪服务客户端,并且获取跟踪服务客户端
            TrackerServer trackerServer = new TrackerClient().getConnection();
            //创建存储服务客户端
            StorageClient storageClient = new StorageClient(trackerServer,null);
            storageClient.delete_file(group_name,remote_filename);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    //获取存储服务信息
    public static FileInfo getStorage(String group_name, String remote_filename){
        try {
            //获取跟踪服务客户端
            TrackerServer trackerServer = new TrackerClient().getConnection();
            //创建存储服务客户端
            StorageClient storageClient = new StorageClient(trackerServer,null);
            FileInfo file_info = storageClient.get_file_info(group_name, remote_filename);
            return file_info;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取多个存储服务器信息
    public static ServerInfo[] getInfo(String groupName, String remoteFilename) throws Exception {
        //获取跟踪服务客户端
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        ServerInfo[] storageServer = trackerClient.getFetchStorages(trackerServer, groupName, remoteFilename);
        return storageServer;
    }
}
